@extends('backend.layouts.page')
@section('content')
    <form class="form-horizontal ajaxForm" action="{{url($baseRoute.'audit')}}">
        <input type="hidden" name="role_tag" value="{{$tag}}"/>
        <div class="table-responsive">
            <table class="table ">
                <tbody>
                @foreach($lis as $top)
                    <tr>
                        <td>
                            <label class="lyear-checkbox checkbox-primary">
                                <input type="checkbox" class="checkbox-parent" dataid="id-{{$top['id']}}">
                                <span> {{$top['name']}}</span>
                            </label>
                        </td>
                    </tr>

                    @if(isset($top['_child']))
                        @foreach($top['_child'] as $controller)
                            <tr>
                                <td class="p-l-20">
                                    <label class="lyear-checkbox checkbox-primary">
                                        <input type="checkbox" class="checkbox-parent checkbox-child"
                                               dataid="id-{{$top['id']}}-{{$controller['id']}}">
                                        <span> {{$controller['name']}}</span>
                                    </label>
                                </td>
                            </tr>
                            @if(isset($controller['_child']))
                                <tr>
                                    <td class="p-l-40">
                                        @foreach($controller['_child'] as $action)
                                            <label class="lyear-checkbox checkbox-primary checkbox-inline">
                                                <input name="permissions[]" type="checkbox" @if($action['checked'])
                                                checked
                                                       @endif class="checkbox-child"
                                                       dataid="id-{{$top['id']}}-{{$controller['id']}}-{{$action['permission_id']}}"
                                                       value="{{$action['permission_id']}}">
                                                <span> {{$action['name']}}</span>
                                            </label>
                                        @endforeach
                                    </td>
                                </tr>
                            @endif
                        @endforeach
                    @endif
                @endforeach
                </tbody>
            </table>
        </div>
        <div class="form-group">
            <div class="col-xs-9">
                <button class="btn btn-primary" type="submit">保存权限</button>
            </div>
        </div>
    </form>
@stop
@push('scripts')
    <script type="text/javascript">
        $(function () {
            //动态选择框，上下级选中状态变化
            $('input.checkbox-parent').on('change', function () {
                var dataid = $(this).attr("dataid");
                $('input[dataid^=' + dataid + '-]').prop('checked', $(this).is(':checked'));
            });
            $('input.checkbox-child').on('change', function () {
                var dataid = $(this).attr("dataid");
                dataid = dataid.substring(0, dataid.lastIndexOf("-"));
                var parent = $('input[dataid=' + dataid + ']');
                if ($(this).is(':checked')) {
                    parent.prop('checked', true);
                    //循环到顶级
                    while (dataid.lastIndexOf("-") != 2) {
                        dataid = dataid.substring(0, dataid.lastIndexOf("-"));
                        parent = $('input[dataid=' + dataid + ']');
                        parent.prop('checked', true);
                    }
                } else {
                    //父级
                    if ($('input[dataid^=' + dataid + '-]:checked').length == 0) {
                        parent.prop('checked', false);
                        //循环到顶级
                        while (dataid.lastIndexOf("-") != 2) {
                            dataid = dataid.substring(0, dataid.lastIndexOf("-"));
                            parent = $('input[dataid=' + dataid + ']');
                            if ($('input[dataid^=' + dataid + '-]:checked').length == 0) {
                                parent.prop('checked', false);
                            }
                        }
                    }
                }
            });
        });
    </script>
@endpush
